package com.avic.rong.course.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.course.activity.CourseDetailActivity;
import com.avic.rong.course.bean.PeiXunListBean;
import com.avic.rong.utils.DateUtils;
import com.avic.rong.utils.DensityUtils;
import com.avic.rong.utils.GlideUtils;
import com.example.minato.minastore.utils.AppUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Chenjiaxin on 2019/8/11 18:19
 * E-Mail Address：16601203965@163.com
 */
public class PeiXunLineClassRvAdapter extends RecyclerView.Adapter<PeiXunLineClassRvAdapter.PeiXunVideoRvViewHolder>{

    private Context context;
    private List<PeiXunListBean.DataBean> dataBeanList = new ArrayList<>();

    public PeiXunLineClassRvAdapter(Context context) {
        this.context = context;
    }

    public void clearData(){
        this.dataBeanList.clear();
    }

    public void setDataBeanList(List<PeiXunListBean.DataBean> dataBeanList) {
        this.dataBeanList.addAll(dataBeanList);
        notifyDataSetChanged();
    }

    public List<PeiXunListBean.DataBean> getDataBeanList() {
        return dataBeanList;
    }

    @NonNull
    @Override
    public PeiXunVideoRvViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.peixun_course_video_item,viewGroup,false);
        return new PeiXunVideoRvViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PeiXunVideoRvViewHolder holder, int i) {

        final PeiXunListBean.DataBean dataBean = dataBeanList.get(i);

        GlideUtils.loaderHanldeRoundImage(context, AppUtils.COURSE_IMAGE_URL + dataBean.getCover(), holder.img,R.mipmap.offline_default, DensityUtils.dp2px(context,6));
//        GlideUtils.loadRoundImage(context,6,AppUtils.COURSE_IMAGE_URL + dataBeanList.get(i).getCover(),R.mipmap.online_default, holder.img);
//        Glide.with(context).load(AppUtils.COURSE_IMAGE_URL + dataBeanList.get(i).getCover()).placeholder(R.mipmap.online_default).into(holder.img);
        holder.title.setText(dataBean.getName());
//        if (TextUtils.isEmpty(dataBean.getBuyCount()+"")){
//            holder.classnum.setText("");
//        }else {
//            holder.classnum.setText("课时:共  "+ dataBean.getBuyCount()+"课时");
//        }
//        String timeExpend = getTimeExpend(dataBean.getStartTime(), dataBean.getEndTime());
//        if (TextUtils.isEmpty(timeExpend)){
//            holder.classtime.setText("");
//        }else {
//            String[] split = timeExpend.split(":");
//            holder.classtime.setText("时长:  "+split[0]+"小时"+split[1]+"分");
//        }

        holder.classnum.setText("开课时间：" + DateUtils.getFormatShort(dataBean.getStartTime()));
        holder.classtime.setText("开课地点：" + dataBean.getCity());//

        holder.price.setText("¥"+ dataBean.getPrice());
        holder.itemView.setOnClickListener(new MyOnClickListener(dataBean));
    }

    @Override
    public int getItemCount() {
        return dataBeanList.size();
    }



    class MyOnClickListener implements View.OnClickListener{
        PeiXunListBean.DataBean dataBean;

        public MyOnClickListener(PeiXunListBean.DataBean dataBean) {
            this.dataBean = dataBean;
        }

        @Override
        public void onClick(View v) {
            StringBuffer sb = new StringBuffer();
            sb.append(dataBean.getType()+"");
            sb.append(dataBean.getId()+"");
            context.startActivity(CourseDetailActivity.getIntent(context,sb.toString(), dataBean.getCover(),""));
        }
    }



    class PeiXunVideoRvViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.peixun_list_img)
        ImageView img;
        @BindView(R.id.peixun_list_title)
        TextView title;
        @BindView(R.id.peixun_list_classnum)
        TextView classnum;
        @BindView(R.id.peixun_list_classtime)
        TextView classtime;
        @BindView(R.id.peixun_list_price)
        TextView price;

        public PeiXunVideoRvViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }


    private String getTimeExpend(long startTime, long endTime){
        //传入字串类型 2016/06/28 08:30
        long longStart = startTime; //获取开始时间毫秒数
        long longEnd = endTime;  //获取结束时间毫秒数
        long longExpend = longEnd - longStart;  //获取时间差

        long longHours = longExpend / (60 * 60 * 1000); //根据时间差来计算小时数
        long longMinutes = (longExpend - longHours * (60 * 60 * 1000)) / (60 * 1000);   //根据时间差来计算分钟数

        return longHours + ":" + longMinutes;
    }

}
